<?php
namespace app\kaoping\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
class Cs extends First{

	public function cost(){
		if (session('atype')==1) {
			$aid=session('aid');
		}else{
			$aid=input('aid');
			$jigou=db('school')->where('aid !=""')->order('convert(name using gbk)')->select();$this->assign('jigou',$jigou);
		}
		$n0=0;$n1=0;$n2=0;$n3=0;$n4=0;$n5=0;$n6=0;$n7=0;
		$t0=0;$t1=0;$t2=0;$t3=0;$t4=0;$t5=0;$t6=0;$t7=0;
		if ($aid!='') {
			$list=db('card')->select();//dump($list);
			if (input('pid')!='') {
				$pinfo=db('pici')->where('id',input('pid'))->find();
				$where='s.sid='.$aid.' and sc.pid="'.input('pid').'"';
			}else{
				$where='s.sid='.$aid;
			}
			if ($list) {
				foreach ($list as $k => $v) {
					$cid=' and sc.cid='.$v['id'];
					$count=db('student_card')->alias('sc')->join('student s','sc.sid=s.id','inner')->where($where.$cid)->count();
					$list[$k]['num']=$count;
					if($v['type']=='CCAT'){
						switch($v['money']){
							case 280:
								$n1+=$count;$t1=280;break;
							case 320:
								$n2+=$count;$t2=320;break;
							case 360:
								$n3+=$count;$t3=360;break;
							case 400:
								$n4+=$count;$t4=400;break;
							case 450:
								$n5+=$count;$t5=450;break;
							case 500:
								$n6+=$count;$t6=500;break;
							case 1500:
								$n7+=$count;$t7=1500;break;
						}
					}elseif($v['type']=='CBDF'){
						if($v['money']==1500){
							$n7+=$count;$t7=1500;
						}else{
							$n0+=$list[$k]['num'];	$t0+=$list[$k]['num']*$v['money'];
						}
					}
					
				}
			}
			$pici=db('pici')->where('aid='.$aid)->order('id desc')->select();
		}
		$p0=$this->cdbf_price($n0,$t0);
		$p1=$this->ccat_price($n1,$t1);
		$p2=$this->ccat_price($n2,$t2);
		$p3=$this->ccat_price($n3,$t3);
		$p4=$this->ccat_price($n4,$t4);
		$p5=$this->ccat_price($n5,$t5);
		$p6=$this->ccat_price($n6,$t6);
		$p7=$this->ccat_price($n7,$t7);
		$total=$p0+$p1+$p2+$p3+$p4+$p5+$p6+$p7;dump($total);
		//return view('',['list'=>$list,'pici'=>$pici,'pid'=>input('pid'),'num'=>$num,'total'=>$total,'discount'=>$discount,'aid'=>input('aid'),'pinfo'=>$pinfo]);
	}

	//CBDF折扣后价格
	function cdbf_price($num=0,$money=0){
		if($num!=0 && $money!=0){
			$total=0;
			switch($num){
				case $num<40:
					$total=$money*0.7;break;
				case $num<80:
					$total=$money*0.65;break;
				case $num<300:
					$total=$money*0.6;break;
				case $num<500:
					$total=$money*0.55;break;
				case 500<$num:
					$total=$money*0.5;break;
				default :
					$total=0;
			}
			return $total;
		}else{
			return 0;
		}
	}
	
	//CCAT折扣后价格
	function ccat_price($num=0,$money=0){
		if($num!=0 && $money!=0){
			$total=0;
			switch($money){
				case 280:
					if($num<80){
						$total=$num*180;
					}else{
						$total=$num*160;
					}
					break;
				case 320:
					if($num<80){
						$total=$num*192;
					}else{
						$total=$num*160;
					}
					break;
				case 360:
					if($num<80){
						$total=$num*216;
					}else{
						$total=$num*180;
					}
					break;
				case 400:
					if($num<80){
						$total=$num*240;
					}else{
						$total=$num*200;
					}
					break;
				case 450:
					if($num<80){
						$total=$num*270;
					}else{
						$total=$num*230;
					}
					break;
				case 500:
					if($num<80){
						$total=$num*300;
					}else{
						$total=$num*200;
					}
					break;
				case 1500:
					$total=$num*1000;
					/*if($num<80){
						$total=$num*1000;
					}else{
						$total=$num*160;
					}*/
					break;
			}
			return $total;
		}else{
			return 0;
		}
	}

	function getPy(){
		$py=get_pinyin('江进科');dump($py);
	}
}